package com.example.print;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.example.lc_print_sdk.PrintUtil;

import com.example.print.util.QRCodeUtil;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class Printer extends CordovaPlugin implements PrintUtil.PrinterBinderListener {
    private PrintUtil printUtil;
    private Context context;


    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        context = cordova.getContext();
    }

    @Override
    protected void pluginInitialize() {
        init();
    }

    public void init() {
        try {
            printUtil=PrintUtil.getInstance (context);
            printUtil.setPrintEventListener (this);
            printUtil.printEnableMark (true);
        } catch (Exception e) {
        }
    }

    @Override
    public void onPrintCallback(int state) {
    }

    @Override
    public void onVersion(String s) {
    }

    public void wirerodpurchaseinorder(String qrcode,String vendor,String specification,String weight,String name,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.wirerodpurchaseinorder(qrcode,vendor,specification,weight,name);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void acidTakeup(String qrcode,String steel,String specification,String weight,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.acidTakeup(qrcode,steel,specification,weight);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void wireProductback(String qrcode,String name,String specification,String weight,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.wireProductback(qrcode,name,specification,weight);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void silkTakeup(String qrcode,String name,String specification,String steel,String length,String weight,String kh,String pcd,String user,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.silkTakeup(qrcode,name,specification,steel,length,weight,kh,pcd,user);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }


    public void gjxtwistwireTakeup(String qrcode,String name,String specification,String steel,String length,String weight,String kh,String pcd,String user,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.gjxtwistwireTakeup(qrcode,name,specification,steel,length,weight,kh,pcd,user);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void heatTakeup(String qrcode,String name,String specification,String steel,String weight,String kh,String pcd,String qd,String zinc,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.heatTakeup(qrcode,name,specification,steel,weight,kh,pcd,qd,zinc);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void qualitysampling(String qrcode,String name,String specification,String steel,String weight,String kh,String pcd,String qd,String zinc,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.qualitysampling(qrcode,name,specification,steel,weight,kh,pcd,qd,zinc);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void wireProduct(String qrcode,String name,String specification,String steel,String weight,String zinc,String time,String qd,String zincBZ,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.wireProduct(qrcode,name,specification,steel,weight,zinc,time,qd,zincBZ);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void steelStrandProduct(String qrcode,String name,String specification,String steel,String weight,String time,String qd,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.steelStrandProduct(qrcode,name,specification,steel,weight,time,qd);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

     public void accessorypurchaseinorder(String code,String name,String specification,String weight,String batch,CallbackContext callbackContext) {
         try {
             Bitmap mBitmap;
             mBitmap = QRCodeUtil.accessorypurchaseinorder(code,name,specification,weight, batch);
             printUtil.printBitmap(mBitmap);
             printUtil.start();
             callbackContext.success();
         } catch (Exception e) {
             callbackContext.error(e.getMessage());

         }
     }

    public void comeprocessinorder(String qrcode,String vendor,String specification,String weight,String customer,String processType,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.comeprocessinorder(qrcode,vendor,specification,weight,customer,processType);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void initSilkwarehouse(String qrcode,String vendor,String specification,String weight,CallbackContext callbackContext) {
        try {
            Bitmap mBitmap;
            mBitmap = QRCodeUtil.initSilkwarehouse(qrcode,vendor,specification,weight);
            printUtil.printBitmap(mBitmap);
            printUtil.start();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error(e.getMessage());

        }
    }

    public void returnorder(String qrcode,String vendor,String specification,String weight,String workplanNumber,CallbackContext callbackContext) {
                try {
                    Bitmap mBitmap;
                    mBitmap = QRCodeUtil.returnorder(qrcode,vendor,specification,weight,workplanNumber);
                    callbackContext.success();
                } catch (Exception e) {
                    callbackContext.error(e.getMessage());

                }
            }

    public void initProductwarehouses(String number,String specification,String weight,String workplanNumber,CallbackContext callbackContext) {
            try {
                Bitmap mBitmap;
                mBitmap = QRCodeUtil.initProductwarehouses(number,specification,weight,workplanNumber);
                printUtil.printBitmap(mBitmap);
                printUtil.start();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());

            }
        }

    public void wirerodcollectoutorder(String number,String specification,String amount,String machine,CallbackContext callbackContext) {
            try {
                Bitmap mBitmap;
                mBitmap = QRCodeUtil.wirerodcollectoutorder(number,specification,amount,machine);
                printUtil.printBitmap(mBitmap);
                printUtil.start();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());

            }
        }

    public void initRopeWirewarehouse(String number,String dcbz,String pm,String gg,String qd,String mc,String mz,String pz,String jz,String czg,String rq,String kh,String xd,String gh,String pcd,String axleLength,CallbackContext callbackContext) {
                        try {
                            Bitmap mBitmap;
                            mBitmap = QRCodeUtil.initRopeWirewarehouse(number,dcbz,pm,gg,qd,mc,mz,pz,jz,czg,rq,kh,xd,gh,pcd,axleLength);
                            printUtil.printBitmap(mBitmap);
                            printUtil.start();
                            callbackContext.success();
                        } catch (Exception e) {
                            callbackContext.error(e.getMessage());

                        }
                    }

    public void initTwistWirewarehouse(String number,String mc,String name,String specification,String jg,String zj,String nx,String bm,String qd,String ty,String date,String user,String length,String sgg,String pcd,String isShort,CallbackContext callbackContext) {
                try {
                    Bitmap mBitmap;
                    mBitmap = QRCodeUtil.initTwistWirewarehouse(number, mc, name, specification, jg, zj, nx, bm, qd, ty, date, user, length, sgg, pcd, isShort);
                    printUtil.printBitmap(mBitmap);
                    printUtil.start();
                    callbackContext.success();
                } catch (Exception e) {
                    callbackContext.error(e.getMessage());

                }
            }


    public void initUniteWirewarehouse(String number,String mc,String name,String specification,String jg,String zj,String nx,String bm,String qd,String ty,String date,String user,String pcd,String sjmc,String isShort,String ddxx,CallbackContext callbackContext) {
                try {
                    Bitmap mBitmap;
                    mBitmap = QRCodeUtil.initUniteWirewarehouse(number, mc, name, specification, jg, zj, nx, bm, qd, ty, date, user, pcd, sjmc, isShort, ddxx);
                    printUtil.printBitmap(mBitmap);
                    printUtil.start();
                    callbackContext.success();
                } catch (Exception e) {
                    callbackContext.error(e.getMessage());

                }
            }

    public void initHDFCwarehouse(String number,String mc,String name,String specification,String jg,String zj,String nx,String bm,String qd,String ty,String date,String user,String machine,String mz,String pz,String jz,String ddxx,CallbackContext callbackContext) {
                try {
                    Bitmap mBitmap;
                    mBitmap = QRCodeUtil.initHDFCwarehouse(number, mc, name, specification, jg, zj, nx, bm, qd, ty, date, user, machine, mz, pz, jz, ddxx);
                    printUtil.printBitmap(mBitmap);
                    printUtil.start();
                    callbackContext.success();
                } catch (Exception e) {
                    callbackContext.error(e.getMessage());

                }
            }


    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            if ("initSilkwarehouse".equals(action)) {
                initSilkwarehouse(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),callbackContext);
                return true;
            }
            if ("wirerodpurchaseinorder".equals(action)) {
                wirerodpurchaseinorder(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),callbackContext);
                return true;
            }
            if ("acidTakeup".equals(action)) {
                acidTakeup(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),callbackContext);
                return true;
            }
            if ("wireProductback".equals(action)) {
                wireProductback(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),callbackContext);
                return true;
            }
            if ("silkTakeup".equals(action)) {
                silkTakeup(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),callbackContext);
                return true;
            }
            if ("gjxtwistwireTakeup".equals(action)) {
                gjxtwistwireTakeup(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),callbackContext);
                return true;
            }
            if ("heatTakeup".equals(action)) {
                heatTakeup(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),callbackContext);
                return true;
            }
            if ("qualitysampling".equals(action)) {
                qualitysampling(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),callbackContext);
                return true;
            }
            if ("wireProduct".equals(action)) {
                wireProduct(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),callbackContext);
                return true;
            }
            if ("steelStrandProduct".equals(action)) {
                steelStrandProduct(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),callbackContext);
                return true;
            }
            if ("accessorypurchaseinorder".equals(action)) {
                accessorypurchaseinorder(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),callbackContext);
                return true;
            }
            if ("comeprocessinorder".equals(action)) {
                comeprocessinorder(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),callbackContext);
                return true;
            }
            if ("returnorder".equals(action)) {
                returnorder(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),callbackContext);
                return true;
            }
            if ("initProductwarehouses".equals(action)) {
                initProductwarehouses(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),callbackContext);
                return true;
            }
            if ("wirerodcollectoutorder".equals(action)) {
                wirerodcollectoutorder(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),callbackContext);
                return true;
            }
            if ("initRopeWirewarehouse".equals(action)) {
                initRopeWirewarehouse(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),args.get(9).toString(),args.get(10).toString(),args.get(11).toString(),args.get(12).toString(),args.get(13).toString(),args.get(14).toString(),args.get(15).toString(),callbackContext);
                return true;
            }
            if ("initTwistWirewarehouse".equals(action)) {
                initTwistWirewarehouse(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),args.get(9).toString(),args.get(10).toString(),args.get(11).toString(),args.get(12).toString(),args.get(13).toString(),args.get(14).toString(),args.get(15).toString(),callbackContext);
                return true;
            }
            if ("initUniteWirewarehouse".equals(action)) {
                initUniteWirewarehouse(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),args.get(9).toString(),args.get(10).toString(),args.get(11).toString(),args.get(12).toString(),args.get(13).toString(),args.get(14).toString(),args.get(15).toString(),callbackContext);
                return true;
            }
            if ("initHDFCwarehouse".equals(action)) {
                initHDFCwarehouse(args.get(0).toString(),args.get(1).toString(),args.get(2).toString(),args.get(3).toString(),args.get(4).toString(),args.get(5).toString(),args.get(6).toString(),args.get(7).toString(),args.get(8).toString(),args.get(9).toString(),args.get(10).toString(),args.get(11).toString(),args.get(12).toString(),args.get(13).toString(),args.get(14).toString(),args.get(15).toString(),args.get(16).toString(),callbackContext);
                return true;
            }
            return false;
        } catch (Exception e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}
